package cn.jbolt.core.controller.base;

import cn.jbolt.core.para.JBoltParaValidator;
import com.jfinal.kit.Kv;
import com.jfinal.upload.UploadFile;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class JBoltFormData{
    private Kv formDataItems;
    private List<UploadFile> uploadFiles;

    public void set(String paramName,Object value){
        this.formDataItems.set(paramName,value);
    }

    public <T> T getAs(String paramName){
        return this.formDataItems.getAs(paramName);
    }

    public Long getLong(String paramName){
        return this.formDataItems.getLong(paramName);
    }

    public Long getLong(String paramName,Long defaultValue){
        return this.formDataItems.getLong(paramName,defaultValue);
    }

    public Object get(String paramName){
        return this.formDataItems.get(paramName);
    }

    public String getStr(String paramName){
        return this.formDataItems.getStr(paramName);
    }

    public String getStr(String paramName,String defaultValue){
        return this.formDataItems.getStr(paramName,defaultValue);
    }

    public Integer getInt(String paramName){
        return this.formDataItems.getInt(paramName);
    }

    public Integer getInt(String paramName,Integer defaultValue){
        return this.formDataItems.getInt(paramName,defaultValue);
    }

    public BigDecimal getBigDecimal(String paramName){
        return this.formDataItems.getBigDecimal(paramName);
    }

    public BigDecimal getBigDecimal(String paramName,BigDecimal defaultValue){
        return this.formDataItems.getBigDecimal(paramName,defaultValue);
    }

    public <T> T getAs(String paramName,T defaultValue){
        return this.formDataItems.getAs(paramName,defaultValue);
    }

    public JBoltFormData() {
        this.formDataItems = Kv.create();
        this.uploadFiles = new ArrayList<>();
    }


    public Kv getFormDataItems() {
        return formDataItems;
    }

    public void setFormDataItems(Kv formDataItems) {
        this.formDataItems = formDataItems;
    }

    public List<UploadFile> getUploadFiles() {
        return uploadFiles;
    }

    public void setUploadFiles(List<UploadFile> uploadFiles) {
        this.uploadFiles = uploadFiles;
    }

    public void addUploadFile(UploadFile uploadFile) {
        this.uploadFiles.add(uploadFile);
    }

    public UploadFile getUploadFile() {
        return this.uploadFiles.get(0);
    }

    public UploadFile getUploadFile(int index){
        return this.uploadFiles.get(index);
    }

    public UploadFile getUploadFile(String paramName){
        return this.uploadFiles.stream().filter(file->paramName.equals(file.getParameterName())).findFirst().orElse(null);
    }

    public boolean isOk(){
        return JBoltParaValidator.hasOk(this.formDataItems,this.uploadFiles);
    }

    public boolean notOk(){
        return !isOk();
    }
}
